package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.sell.dao.CustomerDueSubjectDao;
import com.wing.sell.model.entity.*;
import com.wing.sell.model.entity.CustomerDueSubjectCompany;
import com.wing.sell.model.request.CustomerDueSubjectForm;
import com.wing.sell.model.response.CustomerDueSubjectVO;
import com.wing.sell.service.CustomerDueSubjectCompanyService;
import com.wing.sell.service.CustomerDueSubjectService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 客户尽调科目-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CustomerDueSubjectServiceImpl extends ServiceImpl<CustomerDueSubjectDao, CustomerDueSubject> implements CustomerDueSubjectService {

    @Autowired
    private CustomerDueSubjectCompanyService customerDueSubjectCompanyService;

    @Override
    @Transactional(readOnly = false)
    public int add(CustomerDueSubjectForm customerDueSubjectForm) {

        //校验
        CustomerDueSubject customerDueSubject =new CustomerDueSubject();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CustomerDueSubject.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(customerDueSubjectForm, customerDueSubject, copyOptions);

        int i = baseMapper.insert(customerDueSubject);
        if(i > 0){
            for (CustomerDueSubjectCompany customerDueSubjectCompany : customerDueSubjectForm.getCustomerDueSubjectCompanyList()) {
                customerDueSubjectCompany.setCreator(JwtUtils.getLoginUserId());
                customerDueSubjectCompany.setCreatorName(JwtUtils.getLoginUsername());
                customerDueSubjectCompany.setModifier(JwtUtils.getLoginUserId());
                customerDueSubjectCompany.setModifierName(JwtUtils.getLoginUsername());
                customerDueSubjectCompany.setSubjectId(customerDueSubject.getId());
            }
            customerDueSubjectCompanyService.saveBatch(customerDueSubjectForm.getCustomerDueSubjectCompanyList());
        }
        return i;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean update(CustomerDueSubject customerDueSubject) {
        int i = baseMapper.updateById(customerDueSubject);
        if(i > 0){
            List<CustomerDueSubjectCompany> addCompanys = new ArrayList<>();
            List<CustomerDueSubjectCompany> updCompanys = new ArrayList<>();
            List<Long> delCompanys = new ArrayList<>();
            List<Long> ids = new ArrayList<>();
            for (CustomerDueSubjectCompany customerDueSubjectCompany : customerDueSubject.getCustomerDueSubjectCompanyList()) {
                if(customerDueSubjectCompany.getId() == null){
                    customerDueSubjectCompany.setCreator(JwtUtils.getLoginUserId());
                    customerDueSubjectCompany.setCreatorName(JwtUtils.getLoginUsername());
                    customerDueSubjectCompany.setModifier(JwtUtils.getLoginUserId());
                    customerDueSubjectCompany.setModifierName(JwtUtils.getLoginUsername());
                    customerDueSubjectCompany.setSubjectId(customerDueSubject.getId());
                    addCompanys.add(customerDueSubjectCompany);
                }else{
                    ids.add(customerDueSubjectCompany.getId());
                    updCompanys.add(customerDueSubjectCompany);
                }
            }
            // 查询旧数据
            List<CustomerDueSubjectCompany> oldCompanys = customerDueSubjectCompanyService.list(new QueryWrapper<CustomerDueSubjectCompany>().eq("subject_id", customerDueSubject.getId()));
            for (CustomerDueSubjectCompany oldCompany : oldCompanys) {
                if(!ids.contains(oldCompany.getId())){
                    delCompanys.add(oldCompany.getId());
                }
            }
            if(addCompanys.size() > 0){
                customerDueSubjectCompanyService.saveBatch(addCompanys);
            }
            if(updCompanys.size() > 0){
                customerDueSubjectCompanyService.updateBatchById(updCompanys);
            }
            if(delCompanys.size() > 0){
                customerDueSubjectCompanyService.removeByIds(delCompanys);
            }
        }
        return i > 0;
    }

    @Override
    public List<CustomerDueSubjectVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CustomerDueSubjectVO> pageList4app(Page page, Map<String, Object> params) {
        List<CustomerDueSubjectVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CustomerDueSubjectVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional(readOnly = false)
    public boolean delete(Long customerDueSubjectId) {
        int i = baseMapper.deleteById(customerDueSubjectId);
        if(i > 0){
            customerDueSubjectCompanyService.remove(new QueryWrapper<CustomerDueSubjectCompany>().eq("subject_id", customerDueSubjectId));
        }
        return i > 0;
    }

    @Override
    public int enable(Long id) {
        int i = 0;
        CustomerDueSubject customerDueSubject = baseMapper.selectById(id);
        if(customerDueSubject != null){
            customerDueSubject.setIsDeleted(true);
            i = baseMapper.updateById(customerDueSubject);
        }
        return i;
    }

    @Override
    public int disable(Long id) {
        int i = 0;
        CustomerDueSubject customerDueSubject = baseMapper.selectById(id);
        if(customerDueSubject != null){
            customerDueSubject.setIsDeleted(false);
            i = baseMapper.updateById(customerDueSubject);
        }
        return i;
    }

}
